package com.zzh.phone.hviews;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

import com.zzh.lib.views.adapter.DragBaseAdapter;
import com.zzh.lib.views.adapter.holder.HSuperViewHolder;
import com.zzh.lib.views.drag.OnDragItemTouchCallback;
import com.zzh.phone.hviews.databinding.ActDragImgBinding;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.ItemTouchHelper;

/**
 * Created by ZZH on 2022/5/29.
 *
 * @Date: 2022/5/29
 * @Email: zzh_hz@126.com
 * @QQ: 1299234582
 * @Author: zzh
 * @Description: Grid 拖拽
 */
public class DragAct extends Activity implements OnDragItemTouchCallback.OnDragListener, DragBaseAdapter.OnClickDragPreListener {

    ActDragImgBinding bind;
    private ItemTouchHelper itemTouchHelper;
    DragAdapter adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        bind = ActDragImgBinding.inflate(getLayoutInflater());
        setContentView(bind.getRoot());
        List<ImageModel> list = new ArrayList<>();
        list.add(new ImageModel("https://t7.baidu.com/it/u=1931883710,886308100&fm=193&f=GIF"));
        list.add(new ImageModel("https://t7.baidu.com/it/u=2237872352,3036992004&fm=193&f=GIF"));
        list.add(new ImageModel("https://t7.baidu.com/it/u=3766008746,299106020&fm=193&f=GIF"));
        list.add(new ImageModel("https://t7.baidu.com/it/u=1831846009,1460005090&fm=193&f=GIF"));
        list.add(new ImageModel("https://t7.baidu.com/it/u=3859735106,2767469648&fm=193&f=GIF"));
        list.add(new ImageModel("https://t7.baidu.com/it/u=3632800210,243418919&fm=193&f=GIF"));

        adapter = new DragAdapter(this);
        adapter.addAll(list);
        bind.rvList.setAdapter(adapter);
        itemTouchHelper = new ItemTouchHelper(new OnDragItemTouchCallback(adapter).setOnDragListener(this));
        itemTouchHelper.attachToRecyclerView(bind.rvList);
        adapter.setOnItemDragClickListener(this);
    }

    @Override
    public void onFinishDrag() {
        adapter.notifyDataSetChanged();
        List<ImageModel> dataList = adapter.getDataList();
        dataList.stream().forEach(it -> Log.e("--: ", it.url));
    }

    @Override
    public void onLongClickBook(HSuperViewHolder vh, int position) {
        itemTouchHelper.startDrag(vh);
    }
}
